package cn.edu.zafu.bigdata.bd121.except;

/**
 * 自定义异常demo
 *
 * @author 202211090121
 */
public class CustomExceptionExample {

    /**
     * 通过检查的最小年龄
     */
    private static final int MIN_AGE = 18;

    /**
     * 检查年龄是否有效
     *
     * @param age 年龄
     * @throws InvalidAgeException 年龄无效异常
     */
    public static void checkAge(int age) throws InvalidAgeException {
        if (age < MIN_AGE) {
            throw new InvalidAgeException("年龄不得小于" + MIN_AGE + "岁。");
        }
        System.out.println("年龄有效。");
    }

    public static void main(String[] args) {
        int testAge = 16;
        try {
            checkAge(testAge);
        } catch (InvalidAgeException e) {
            System.err.println("错误: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("未知错误: " + e.getMessage());
        }
    }
}
